package com.youlian.view.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.youlian.activity.R;
import com.youlian.common.Configure;
import com.youlian.manager.model.CardsResult;
import com.youlian.utils.Utils;
import com.youlian.utils.bitmap.ImageLoader;
import com.youlian.utils.bitmap.ImageLoader.DownImageCallback;

/**
 * @author simon
 * @proName youlian
 * @version 1.0
 * @Data 2012-9-28 ����12:08:46
 *
   <b>Comment...</b>
 */
public class HomeAdapter extends BaseAdapter{
	private Context mContext;
	private LayoutInflater mInflater;
	private ArrayList<CardsResult> resultList;
	private ImageLoader imageLoader;

	public HomeAdapter(Context mContext,ArrayList<CardsResult> reuslts){
		this.mContext = mContext;
		imageLoader = new ImageLoader(mContext,Configure.screenWidth/3,R.drawable.icon_2);
		resultList = new ArrayList<CardsResult>();
		mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if(Utils.notNull(reuslts)){
			resultList.addAll(reuslts);
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return resultList.size();
	}

	@Override
	public CardsResult getItem(int arg0) {
		// TODO Auto-generated method stub
		return resultList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		final ViewCache vc;
		if (convertView == null) {
			vc = new ViewCache();
			convertView = mInflater.inflate(R.layout.home_listview_item, null);
			vc.stroe_img = (ImageView)convertView.findViewById(R.id.stroe_img);
			vc.is_store = (ImageView)convertView.findViewById(R.id.is_store);
			vc.follow_active = (ImageView)convertView.findViewById(R.id.follow_active);
//			vc.follow_card = (ImageView)convertView.findViewById(R.id.follow_card);
			vc.follow_interest = (ImageView)convertView.findViewById(R.id.follow_interest);
			vc.follow_union = (ImageView)convertView.findViewById(R.id.follow_union);
			vc.stroe_name_text = (TextView)convertView.findViewById(R.id.card_name_text);
			vc.card_content_text = (TextView)convertView.findViewById(R.id.card_content_text);
			convertView.setTag(vc);
		}else {
			vc = (ViewCache) convertView.getTag();
		}

		final CardsResult cardsResult = this.getItem(position);
		
		System.out.println("---cardsResult.getCard_name()-----:"+cardsResult.getCard_name());
		vc.stroe_name_text.setText(cardsResult.getCard_name());
		vc.card_content_text.setText(cardsResult.getCard_content());
		
		if(Configure.cardType.equals(cardsResult.getIs_store_club()))
			vc.is_store.setImageResource(R.drawable.nucleus_store_a);
		else
			vc.is_store.setImageResource(R.drawable.nucleus_store_b);
		
		if(Configure.cardType.equals(cardsResult.getActivatedPic()))
			vc.follow_active.setImageResource(R.drawable.active_a);
		else
			vc.follow_active.setImageResource(R.drawable.active_b);
		
//		if(Configure.cardType.equals(cardsResult.getIs_card()))
//			vc.follow_card.setImageResource(R.drawable.club_card_a);
//		else
//			vc.follow_card.setImageResource(R.drawable.club_card_b);
		
		if(Configure.cardType.equals(cardsResult.getActivatedPic()))
			vc.follow_interest.setImageResource(R.drawable.interest_store_a);
		else
			vc.follow_interest.setImageResource(R.drawable.interest_store_b);
		
		if(Configure.cardType.equals(cardsResult.getIs_league()))
			vc.follow_union.setImageResource(R.drawable.store_union_a);
		else
			vc.follow_union.setImageResource(R.drawable.store_union_b);
		
		String fileUrl = null;
		if(Configure.cardType.equals(cardsResult.getIs_card())){
			fileUrl = cardsResult.getActivatedPic();
		}else{
			fileUrl = cardsResult.getNonactivatedPic();
		}
		if(Utils.notNull(fileUrl)){
			imageLoader.DisplayImage(fileUrl, vc.stroe_img, new DownImageCallback() {
	
				@Override
				public void downCallback(final String imageUrl, final boolean status) {
					// TODO Auto-generated method stub
					if(status){
					}else{
	//					MyLog.error(getClass(), "************************下载失败***************************:"+status+"   url:"+imageUrl);
						vc.stroe_img.setImageResource(R.drawable.icon_2);
					}
				}
				});
		}
		
		return convertView;
	}
	/**
	 * 设置查看全部商品数据源
	 * @param data
	 * @return
	 */
	public void addAllDataList(ArrayList<CardsResult> data){
		if(resultList!=null	&& resultList.size()>0){
			resultList.clear();
		}else{
			resultList = new ArrayList<CardsResult>();
		}
		resultList.addAll(data);
		startDownLoad();
		this.notifyDataSetChanged();
	}
	
	private class ViewCache {
		public TextView stroe_name_text;
		public TextView card_content_text;
		public ImageView stroe_img,is_store,follow_active,follow_union,follow_interest;
	}
	public void stopDownLoad(){
		if(imageLoader!=null){
			imageLoader.stopDown();
		}
	}

	public void startDownLoad(){
		if(imageLoader!=null){
			imageLoader.startDown();
		}
	}
	public void onDestory(){
		stopDownLoad();
	}
}
